package com.rongzhe.house.house.controller;

import com.rongzhe.house.common.constants.Constants;
import com.rongzhe.house.common.resp.ApiResult;
import com.rongzhe.house.house.criteria.HouseCriteria;
import com.rongzhe.house.house.entity.HouseDelegationApply;
import com.rongzhe.house.house.entity.HouseDetail;
import com.rongzhe.house.house.entity.HouseOverview;
import com.rongzhe.house.house.entity.HouseRentPaymentConfig;
import com.rongzhe.house.house.request.HouseRequest;
import com.rongzhe.house.house.service.HouseService;
import com.rongzhe.house.login.entity.User;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by hggxg on 2017/11/18.
 */
@Slf4j
@RestController
@RequestMapping("house")
public class HouseController {

    @Autowired
    private HouseService houseService;

    @ApiOperation(value = "委托申请", notes = "房源", httpMethod = "POST")
    @RequestMapping(value = "/delegationApply", method = RequestMethod.POST)
    @ApiImplicitParam(name = "delegationApply", value = "申请", required = true, dataType = "HouseDelegationApply")
    public ApiResult delegationApply(HouseDelegationApply delegationApply,HttpServletRequest request) {
        //User user = (User) request.getSession().getAttribute(Constants.USER_LOGIN_KEY);
        ApiResult apiResult = new ApiResult();
       /* if(null==user){
            apiResult.setCode(-1);
            apiResult.setMessage("未登录");
        }else{*/
            apiResult.setData(houseService.delegationApply(delegationApply,null));
            apiResult.setCode(200);
            apiResult.setMessage("操作成功");
        //}
        return apiResult;
    }

    @ApiOperation(value = "生活服务", notes = "房源", httpMethod = "GET")
    @RequestMapping(value = "/lifeService/list", method = RequestMethod.GET)
    @ApiImplicitParam(name = "type", value = "房子ID", required = true, dataType = "Integer")
    public ApiResult lifeServiceList(@RequestParam(value = "type") Integer type) {
        ApiResult apiResult = new ApiResult();
        apiResult.setData(houseService.getLifeService(type));
        return apiResult;
    }

    @ApiOperation(value = "收藏", notes = "房源", httpMethod = "POST")
    @RequestMapping(value = "/docollection", method = RequestMethod.POST)
    @ApiImplicitParam(name = "houseRequest", value = "房子ID", required = true, dataType = "HouseRequest")
    public ApiResult collection(HouseRequest houseRequest, HttpServletRequest request, HttpServletResponse response) throws  Exception{
        User user = (User) request.getSession().getAttribute(Constants.USER_LOGIN_KEY);
        ApiResult apiResult = new ApiResult();
        if(null==user){
            apiResult.setCode(-1);
            apiResult.setMessage("未登录");
            return apiResult;
        }else{
            apiResult.setData(houseService.collection(houseRequest.getHouseId(),user.getId()));
            apiResult.setCode(200);
            apiResult.setMessage("操作成功");
            return apiResult;
        }
    }

    @ApiOperation(value = "收藏", notes = "房源", httpMethod = "POST")
    @RequestMapping(value = "/collectioned", method = RequestMethod.POST)
    @ApiImplicitParam(name = "houseRequest", value = "房子ID", required = true, dataType = "HouseRequest")
    public ApiResult hasCollection(HouseRequest houseRequest, HttpServletRequest request, HttpServletResponse response) throws  Exception{
        User user = (User) request.getSession().getAttribute(Constants.USER_LOGIN_KEY);
        ApiResult apiResult = new ApiResult();
        if(null==user){
            apiResult.setCode(-2);
            apiResult.setMessage("未登录");
            return apiResult;
        }else{
            apiResult.setData(houseService.hasCollection(houseRequest.getHouseId(),user.getId()));
            apiResult.setCode(200);
            apiResult.setMessage("操作成功");
            return apiResult;
        }
    }

    @ApiOperation(value = "房源检索", notes = "房源", httpMethod = "GET")
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    @ApiImplicitParam(name = "houseCriteria", value = "检索条件", required = true, dataType = "HouseCriteria")
    public ApiResult<List<HouseOverview>> houseQuery(@RequestBody HouseCriteria houseCriteria) {

        ApiResult apiResult = new ApiResult();


        apiResult.setData(houseService.queryHouseByCriteria(houseCriteria));


        return apiResult;

    }

    @ApiOperation(value = "首页优质房源获取", notes = "首页", httpMethod = "GET")
    @RequestMapping(value = "/excellent/query", method = RequestMethod.GET)
    @ApiImplicitParam(name = "houseCriteria", value = "检索条件", required = true, dataType = "HouseCriteria")
    public ApiResult<List<HouseOverview>> houseExcellentQuery(@RequestBody HouseCriteria houseCriteria) {
        ApiResult apiResult = new ApiResult();

        apiResult.setData(houseService.getExcellentHouse(houseCriteria.getCityId()));


        return apiResult;
    }

    @ApiOperation(value = "详情", notes = "房源", httpMethod = "GET")
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ApiImplicitParam(name = "houseId", value = "房子ID", required = true, dataType = "String")
    public ApiResult<HouseDetail> detail(@RequestParam(value = "houseId") Integer houseId) {

        ApiResult apiResult = new ApiResult();

        apiResult.setData(houseService.detail(houseId));


        return apiResult;
    }

    @ApiOperation(value = "可选支付方式（月，季，年）", notes = "房源", httpMethod = "GET")
    @RequestMapping(value = "/payConfig", method = RequestMethod.GET)
    @ApiImplicitParam(name = "houseId", value = "房子ID", required = true, dataType = "String")
    public ApiResult<List<HouseRentPaymentConfig>> payConfig(@RequestParam(value = "houseId") Integer houseId) {

        ApiResult apiResult = new ApiResult();

        apiResult.setData(houseService.getHouseRentPayConfigListByHouseId(houseId));


        return apiResult;
    }

}
